package com.nbsystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbsystem.entity.ElcComment;
import com.nbsystem.entity.SysPer;
import com.nbsystem.entity.SysRole;
import com.nbsystem.entity.SysUser;
import com.nbsystem.service.ElcCommentService;
import com.nbsystem.service.SysPerService;
import com.nbsystem.service.SysRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (SysRole)表控制层
 *
 * @author makejava
 * @RestController 证明返回的都是json字符串
 * @since 2021-06-01 16:29:59
 */

@RestController
@RequestMapping("sysRole")
public class SysRoleController {
    /**
     * 服务对象
     */
    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysPerService sysPerService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SysRole selectOne(Integer id) {
        return this.sysRoleService.queryById(id);
    }


//模糊查询
    @RequestMapping("list")
    public PageInfo<SysRole> queryAll(SysRole sysrole,Integer rows,Integer page) {
        PageHelper.startPage(page,rows);
        List<SysRole> list=sysRoleService.queryAll(sysrole);
        PageInfo<SysRole> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @RequestMapping("queryAll")
    public List<SysRole> quertAl(SysRole sysrole){
        return this.sysRoleService.queryAll(sysrole);
    }


    @RequestMapping("goBack")
    public PageInfo<SysRole> goBack(SysRole sysrole,Integer rows,Integer page) {
        PageHelper.startPage(page,rows);
        List<SysRole> list = sysRoleService.goBack(sysrole);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /*
    添加角色信息
     */
    @RequestMapping("add")
    public String addSysRole(SysRole sysRole){
        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(new Date());
        sysRole.setValid(1);
        sysRoleService.insert(sysRole);
        return "success";
    }

    /*
    删除用户信息
     */
    @RequestMapping("delete/{id}")
    public  String deleteSysUser(@PathVariable Integer id){
        sysRoleService.deleteById(id);
        return "success";
    }


    @RequestMapping("deletes/{ids}")
    public String deleteSysUsers(@PathVariable Integer[] ids){
        sysRoleService.deleteByIds(ids);
        return "success";
    }

    @RequestMapping("queryExl")
    public List<SysRole> queryAll(SysRole sysrole){
        return sysRoleService.queryAll(sysrole);
    }


    @RequestMapping("goBackById/{id}")
    public  String goBackSysRole(@PathVariable Integer id){
        sysRoleService.updateById(id);
        return "success";
    }


    /**
     *  根据用户据的ID查询权限
     * @param userID  用户的ID
     * @return   对象列表
     */
//    @RequestMapping("queryRoleByUserID")
//    public List<SysRole> queryRoleByUserId(Integer userID){
//        LoginUser loginUser = loginService.loginRolePer(userID);
//        List<SysRole> sysRoles = null;
//        try {
//            sysRoles = loginUser.getSysRoles();
//        }catch (Exception e){
//
//        }
//        return sysRoles;
//    }


    /**
     *   根据用户的ID修改用户的角色和数据权限
     * @param userID   用户id
     * @param roles   修改完成的角色
     * @param typeIds  修改完成的数据权限
     * @return   成功
     */
    @RequestMapping("updateRoleAndType")
    public String updateRoleAndType(Integer userID, Integer roles[],Integer typeIds[]){

        sysRoleService.updateRoleAndType(userID, roles, typeIds);

        return "success";
    }


    @RequestMapping("querySysPer")
    public List<SysPer> querySysPer(SysPer SysPer){
        return this.sysPerService.queryAll(SysPer);
    }
}
